package com.atguigu.yygh.controller;

import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.service.HospitalSetService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "3.服务熔断hystrix演示")
@RestController
@RequestMapping("/api/hosp")
public class hystrixController {


    @Autowired
    private HospitalSetService hospitalSetService;

    //3 根据id获取医院设置
    @ApiOperation(value = "hystrix根据id获取医院")
    @GetMapping("/hystrix/{id}")
    @HystrixCommand(fallbackMethod = "queryByHystrix")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet==null){
            throw new RuntimeException("id==>"+id+",不存在该用户，或者用户信息无法查到");
        }
        return Result.ok(hospitalSet);
    }

    //备选方法,这是服务熔断措施
    public Result queryByHystrix(@PathVariable("id")Long id){
        return Result.ok("不存在该用户,null--Hystrix");
    }

}
